package com.surge;



import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDataTest {

	private ApplicationContext ctx = null;
	private PersonRepsotory personRepsotory = null;
	
	{
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		personRepsotory = ctx.getBean(PersonRepsotory.class);
	}
	
	
	
	// 测试是否连接数据库
	@Test
	public void testDataSource() throws SQLException {
		DataSource dataSource = ctx.getBean(DataSource.class);
		System.out.println(dataSource.getConnection());
	}
	
	// 测试创建数据表是否成功
	@Test
	public void testJpa(){
		
	}
	
	// 查询
	@Test
	public void testHelloWorldSpringData() throws FileNotFoundException, IOException, InstantiationException, IllegalAccessException{
		
		
		
		System.out.println(personRepsotory.getClass().getName());
		
		Person person = personRepsotory.getByLastName("AA");
		System.out.println(person);
	}
	
	/**
	 * personRepsotory方法名按一定规则命名
	 */
	@Test
	public void testKeyWords(){
		
		List<Person> persons = personRepsotory.getByLastNameStartingWithAndIdLessThan("X", 10);
		System.out.println(persons);
		
		persons = personRepsotory.getByLastNameEndingWithAndIdLessThan("X", 10);
		System.out.println(persons);
		
		persons = personRepsotory.getByEmailInAndBirthLessThan(Arrays.asList("AA@gg.com", "FF@gg.com", 
				"SS@atguigu.com"), new Date());
		
		System.out.println(persons.size());
		
	}
	
	
	@Test
	public void testKeyWords2(){
		
		//5. 支持属性的级联查询. 若当前类有符合条件的属性, 则优先使用,
		//   而不使用级联属性. 若需要使用级联属性, 则属性之间使用 _ 进行连接.
		/*
		 * 在Person中又 addressid属性 以及配置了多对一个 address对象 （ADDRESS_ID）
		 * 这时，使用getByAddressIdGreaterThan，用的是addressid属性（ADD_ID）。要想使用级联属性就需要用下面的方法
		 */
		List<Person> persons = personRepsotory.getByAddress_IdGreaterThan(1);
		System.out.println(persons);
	}
	
	
	
	
}
